/**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.mall.biz.maintain.service.impl;

import co.yixiang.common.core.utils.FileUtil;
import co.yixiang.common.mproot.common.service.impl.BaseServiceImpl;
import co.yixiang.common.mproot.common.utils.QueryHelpPlus;
import co.yixiang.common.mproot.domain.PageResult;
import co.yixiang.common.mproot.dozer.service.IGenerator;
import co.yixiang.common.security.domain.JwtUser;
import co.yixiang.common.security.utils.SecurityUtils;
import co.yixiang.mall.biz.maintain.mapper.YxShippingMaintainMapper;
import co.yixiang.mall.biz.maintain.service.YxShippingMaintainService;
import co.yixiang.mall.common.maintain.domain.YxShippingMaintain;
import co.yixiang.mall.common.maintain.dto.YxShippingMaintainDto;
import co.yixiang.mall.common.maintain.dto.YxShippingMaintainQueryCriteria;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author hupeng
 * @date 2021-03-17
 */
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxShippingMaintain")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxShippingMaintainServiceImpl extends BaseServiceImpl<YxShippingMaintainMapper, YxShippingMaintain> implements YxShippingMaintainService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public PageResult<YxShippingMaintainDto> queryAll(YxShippingMaintainQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxShippingMaintain> page = new PageInfo<>(queryAll(criteria));
        return generator.convertPageInfo(page, YxShippingMaintainDto.class);
    }


    @Override
    //@Cacheable
    public List<YxShippingMaintain> queryAll(YxShippingMaintainQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxShippingMaintain.class, criteria));
    }


    @Override
    public void download(List<YxShippingMaintainDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxShippingMaintainDto yxShippingMaintain : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("修改人ID", yxShippingMaintain.getUserId());
            map.put("修改人名称", yxShippingMaintain.getUserName());
            map.put("平台维护费率%", yxShippingMaintain.getRate());
            map.put("状态 1，开启；0，关闭", yxShippingMaintain.getStatus());
            map.put("创建时间", yxShippingMaintain.getCreateTime());
            map.put("修改时间", yxShippingMaintain.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public int calculate(Integer money) {
        int ret = 0;
        if (money!=0) {
            YxShippingMaintain byId = this.getById("1");
            if (byId.getStatus() == 1) {
                Long rate = byId.getRate();
                double ratio = rate.doubleValue()*0.01;
                ret = (int) (money -  money * ratio);
                return ret;
            }
        }
        return ret;
    }

    @Override
    public List<YxShippingMaintainDto> show() {
            List<YxShippingMaintainDto> list= new ArrayList<>();
        try {
            YxShippingMaintain yxShippingMaintain = this.getById("1");
            YxShippingMaintainDto convert = generator.convert(yxShippingMaintain, YxShippingMaintainDto.class);
            list.add(convert);
            System.out.println(list.get(0).getUpdateTime());
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void updateMaintain(YxShippingMaintain yxShippingMaintain) {
        //获取用户信息
        JwtUser jwtUser = SecurityUtils.getLoginUser();
        lambdaUpdate().eq(YxShippingMaintain::getId, "1").set(YxShippingMaintain::getRate,yxShippingMaintain.getRate()).set(YxShippingMaintain::getUserId, jwtUser.getId())
                .set(YxShippingMaintain::getUserName,jwtUser.getUsername()).set(YxShippingMaintain::getStatus, yxShippingMaintain.getStatus()).set(YxShippingMaintain::getUpdateTime, new Date()).update();

    }
}
